package lp.polimi.treefinder;

import java.io.File;

import lp.polimi.treefinder.utils.IntentUtils;
import lp.polimi.treefinder.utils.UIUtils;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/**
 * The app's main view, from which the user can take the picture of the leaf or load an image from file.
 */
public class TaggingFragment extends Fragment {

	private TaggingFragmentListener mListener;

	public TaggingFragment() {
		// Required empty public constructor
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (TaggingFragmentListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity+" must implement TaggingFragmentListener");
		}
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.tagging, menu);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_tagging, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		
		super.onActivityCreated(savedInstanceState);
		
		//The big button to take the photo.
		ImageButton takePhotoButton = (ImageButton) getActivity().findViewById(R.id.leafButton);
		takePhotoButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (!IntentUtils.hasFeature(getActivity(), PackageManager.FEATURE_CAMERA)) {
					Toast.makeText(getActivity(), R.string.no_camera, Toast.LENGTH_LONG).show();
					return;
				}
				startCamera();
			}
		});

		TextView touchTextView = (TextView) getActivity().findViewById(R.id.touch_textview);  
		Typeface font = Typeface.createFromAsset(getActivity().getAssets(), "ITCEDSCR.TTF");  
		touchTextView.setTypeface(font);
	}

	/**
	 * Uses an intent to start the Camera app and triggers the calling activity for
	 * processing the result.
	 */
	private void startCamera() {
		if (IntentUtils.isIntentAvailable(getActivity(), MediaStore.ACTION_IMAGE_CAPTURE)) {
			UIUtils.lockScreenRotation(getActivity());
			Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES), "tmp.jpg")));
			startActivityForResult(cameraIntent, MainActivity.CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
		} else {
			Toast.makeText(getActivity(), R.string.camera_unavailable, Toast.LENGTH_LONG).show();
		}
	}

	@Override
	/**
	 * Called after the picture has been taken or loaded from file.
	 */
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		mListener.processImage(requestCode, resultCode, data);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.load_file:
			loadFromFile();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}
	
	/**
	 * Load a picture from file.
	 */
	private void loadFromFile() {
		Intent intent = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
		intent.setType("image/*");
		startActivityForResult(intent, MainActivity.IMAGE_SELECT_CODE);
	}


	public interface TaggingFragmentListener {
		public void processImage (int requestCode, int resultCode, Intent data);
	}

}
